package net.melon.page.inst.struts2;

import javax.annotation.Resource;

import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.article.pojo.Article;
import net.melon.page.inst.service.ModuleService;
import net.melon.page.pojo.ModuleLink;
import net.melon.page.pojo.ModuleLinkType;
import net.melon.page.pojo.ModuleLinkType.ModuleLinkTypeType;
import net.melon.page.pojo.dto.PageDto;
import net.melon.product.pojo.Product;
import net.melon.uploader.Uploader;

public class ModuleLinkAction extends AbstractPluginPageAction<ModuleLink, ModuleLink>{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4606928488610401559L;


	private int moduleLinkTypeId;
	
	private ModuleLinkType type;
	
	private int channel;
	
	
	@Resource
	private ModuleService moduleService;
	
	
	@Override
	protected void doQueryPage() {
		pagination = moduleService.getModuleLinkPage(pagination,moduleLinkTypeId);
	}
	
	@Override
	protected void afterQueryPage() {
		for(Object obj : pagination.getRows()){
			ModuleLink link = (ModuleLink) obj;
			if(link.getType().getType()==ModuleLinkTypeType.PRODUCT.getType()){
				Product product = defaultService.get(link.getValue(), Product.class);
				link.setDto(new PageDto(product.getId(),product.getName()));
			}else if(link.getType().getType()==ModuleLinkTypeType.ARTICLE.getType()){
				Article article = defaultService.get(Integer.valueOf(link.getValue()), Article.class);
				link.setDto(new PageDto(String.valueOf(article.getId()),article.getTitle()));
			}
		}
	}
	
	@Override
	protected void doDelete() throws Exception {
		moduleService.deleteModuleLinkById(Integer.valueOf(deletes));
	}
	@Override
	public String form() {
		type = defaultService.get(moduleLinkTypeId, ModuleLinkType.class);
		return SUCCESS;
	}
	
	@Override
	protected void doAdd() throws Exception {
		moduleService.addModuleLink(entity);
	}
	
	@Override
	protected void doGetEntity() {
		entity = moduleService.getModuleLinkWithType(Integer.valueOf(id));
		if(entity.getType().getType()==ModuleLinkTypeType.PRODUCT.getType()){
			Product product = defaultService.get(entity.getValue(), Product.class);
			entity.setDto(new PageDto(product.getId(),product.getName()));
		}else if(entity.getType().getType()==ModuleLinkTypeType.ARTICLE.getType()){
			Article article = defaultService.get(Integer.valueOf(entity.getValue()), Article.class);
			entity.setDto(new PageDto(String.valueOf(article.getId()),article.getTitle()));
		}
	}
	@Override
	protected void doUpdate() throws Exception {
		moduleService.updateModuleLink(entity);
	}
	
	public String getDownUrl() {
		return Uploader.getDownUrl(ModuleLink.MODULE_LINK_ICON);
	}
	public ModuleLink getEntity() {
		return entity;
	}

	public void setEntity(ModuleLink entity) {
		this.entity = entity;
	}

	public int getModuleLinkTypeId() {
		return moduleLinkTypeId;
	}

	public void setModuleLinkTypeId(int moduleLinkTypeId) {
		this.moduleLinkTypeId = moduleLinkTypeId;
	}

	public ModuleLinkType getType() {
		return type;
	}

	public void setType(ModuleLinkType type) {
		this.type = type;
	}

	public int getChannel() {
		return channel;
	}

	public void setChannel(int channel) {
		this.channel = channel;
	}
	
}
